#include "playlist.h"

Playlist::Playlist() :
	isSaved(false),
	empty(true),
	endoflist(true),
	sum(0),
	index(0),
	played(0)
{
	playlistPath = QCoreApplication::applicationDirPath() + "\\default.xml";
}

Playlist::~Playlist()
{
	savePlaylist(playlistPath);
}

void Playlist::loadPlaylist(QString path)
{
	if (!path.isEmpty())
		this->playlistPath = path;

	QFile file(playlistPath);

	if (!file.exists())
		return;

	file.open(QFile::ReadOnly | QFile::Text);
	QDomDocument doc;
	doc.setContent(&file);

	QDomElement outer = doc.firstChildElement().firstChildElement();
	QDomElement inner = outer.firstChildElement();

	items.clear();
	theitem tmp;

	while (!outer.isNull())
	{
		tmp.filepath = inner.text();

		inner = inner.nextSiblingElement();
		tmp.filename = inner.text();

		inner = inner.nextSiblingElement();
		tmp.title = inner.text();

		inner = inner.nextSiblingElement();
		tmp.artist = inner.text();

		inner = inner.nextSiblingElement();
		tmp.album = inner.text();

		for (int i = 0; i < 14; ++ i)
		{
			inner = inner.nextSiblingElement();
			tmp.pos[i] = inner.text().toInt();
		}

		items.push_back(tmp);
		emit itemLoaded(tmp.filename);

		outer = outer.nextSiblingElement();
		inner = outer.firstChildElement();
	}

	sum = items.size();
	empty = false;
	endoflist = false;
	file.close();
}

void Playlist::savePlaylist(QString path)
{
	if (!path.isEmpty())
		this->playlistPath = path;
	QFile file(playlistPath);

	if (empty) {
		file.remove();
		return;
	}

	QDomDocument doc;
	QDomElement root;
	QDomElement key;
	QDomElement tag;
	QDomText text;
	root = doc.createElement("playlist");
	doc.appendChild(root);
	for (int i = 0; i < sum; ++i)
	{
		key = doc.createElement("item");
		root.appendChild(key);

		tag = doc.createElement("filepath");
		text = doc.createTextNode(items[i].filepath);
		tag.appendChild(text);
		key.appendChild(tag);

		tag = doc.createElement("filename");
		text = doc.createTextNode(items[i].filename);
		tag.appendChild(text);
		key.appendChild(tag);

		tag = doc.createElement("title");
		text = doc.createTextNode(items[i].title);
		tag.appendChild(text);
		key.appendChild(tag);

		tag = doc.createElement("artist");
		text = doc.createTextNode(items[i].artist);
		tag.appendChild(text);
		key.appendChild(tag);

		tag = doc.createElement("album");
		text = doc.createTextNode(items[i].album);
		tag.appendChild(text);
		key.appendChild(tag);

		for (int j = 0; j < 14; ++j)
		{
			tag = doc.createElement("pos");
			text = doc.createTextNode(QString::number(items[i].pos[j]));
			tag.appendChild(text);
			key.appendChild(tag);
		}

	}

	file.open(QFile::WriteOnly | QFile::Text);
	QTextStream stream(&file);
	doc.save(stream, 4);
	file.close();
}

/**********************************************************************************************************/
void Playlist::reset(void)
{
	index = 0;
	if (!empty)
		endoflist = false;
}

void Playlist::setIndex(int index)
{
	this->index = index;
}

void Playlist::movIndex(int t)
{
	index += t;

	if (index < 0)
		index = sum-1;

	if (index >= sum)
		index = 0;
}

void Playlist::addItem(QString filepath)
{
	if (empty) {
		empty = false;
		if (endoflist) {
			endoflist = false;
			index = 0;
		} else
			index = -1;
	}

	++sum;

	QFileInfo file(filepath);
	QString filename(file.baseName());

	theitem item;
	item.filepath = filepath;
	item.filename = filename;

	for (int i = 0; i < 14; ++i)
	{
		item.pos[i] = 0;
	}

	item.title = "NULL";
	item.album = "NULL";
	item.artist = "NULL";

	items.push_back(item);
}

void Playlist::delItem(int index)
{
	if (sum == 0)
		return;

	sum-=1;

	if (index <= this->index)
		--(this->index);

	if (sum == 0) {
		empty = true;
		endoflist = true;
	}

	items.erase(items.begin() + index);
}

void Playlist::setItemPoses(unsigned int *pos, QString filepath)
{
	vector<theitem>::iterator it;
	for (it = items.begin(); it != items.end(); ++it) {
		if (it->filepath != filepath)
			continue;
		if (it->pos[1] != 0)
			continue; // same name?
		for (int i = 1; i < 14; ++i)
			it->pos[i] = pos[i-1];
		break;
	}
}

void Playlist::setItemTags(QString title, QString artist, QString album)
{
	items[sum-1].title = title;
	items[sum-1].artist = artist;
	items[sum-1].album = album;
}

/**********************************************************************************************************/
int Playlist::sumItems(void)
{
	return sum;
}

bool Playlist::isEmpty(void)
{
	return empty;
}

bool Playlist::isEndOfList(void)
{
	return endoflist;
}

QString Playlist::itemPath(int index)
{
	return items[index].filepath;
}

QString Playlist::itemPath(void)
{
	if (index < 0) //!!!!
		index = 0;
	return items[index].filepath;
}

QString Playlist::itemTag(int i, int j)
{
	if (i < 0)
		i = 0;
	switch (j)
	{
	case 0:
		return items[i].title;
	case 1:
		return items[i].artist;
	case 2:
		return items[i].album;
	default :
		return "";
	}
}

int Playlist::itemPos(int i)
{
	return items[index].pos[i];
}

int Playlist::itemType(int index)
{
	if (index < 0)
		index = 0;

	if (items[index].pos[1] == 0) {
		if (items[index].title == "NULL")
			return -1;
		else
			return 0;
	} else {
		if (items[index].pos[12] == 0)
			return 1;
		else
			return 2;
	}
}

void Playlist::whileItemFinished(void)
{
	++index;

	if (index >= sum) {
		endoflist = true;
		index = 0;
	}

	emit itemFinished();
}
